package com.qiankun.tang.ui.view;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.common.library.listener.OnAppListener;
import com.common.library.ui.view.ShadeTextView;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.ADBean;
import com.qiankun.tang.ui.view.adapter.ADPagerAdapter;

import java.util.List;

/**
 * @author txf
 * @Title 不自动切换的广告小空间
 * @package com.common.library.ui.view
 * @dae 2017/6/8 0008
 */

public class ADWidget extends RelativeLayout implements OnAppListener.OnInteractionListener, ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager mViewPager;
    private LinearLayout mIndicatorLayout;
    private ShadeTextView mTitle;
    private ShadeTextView mSubTitle;

    private ADPagerAdapter mADPagerAdapter;
    LinearLayout.LayoutParams lp;
    List<ADBean> mDatas;
    private int IndicatorSelectedColor;
    private int IndicatorUnSelectedColor;
    private OnClickListaner l;

    private ADVidoVeiw mADVidoVeiw;

    public ADWidget(@NonNull Context context) {
        super(context);
        init();
    }

    public ADWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ADWidget(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_adwidget, this);
        mViewPager = (ViewPager) findViewById(R.id.view_adwidget_viewPager);
        mIndicatorLayout = (LinearLayout) findViewById(R.id.view_adwidget_indicatorLayout);
        mSubTitle = (ShadeTextView) findViewById(R.id.view_adwidget_subtitle);
        mTitle = (ShadeTextView) findViewById(R.id.view_adwidget_title);

        findViewById(R.id.view_adwidget_search).setOnClickListener(this);

        mViewPager.setAdapter(mADPagerAdapter = new ADPagerAdapter(this));
        mViewPager.addOnPageChangeListener(this);

        IndicatorSelectedColor = R.drawable.round_ffffff;
        IndicatorUnSelectedColor = R.drawable.round_50ffffff;
    }
    public void setPageTransformer(boolean reverseDrawingOrder, ViewPager.PageTransformer transformer) {
        mViewPager.setPageTransformer(reverseDrawingOrder, transformer);
    }
    public void setData(List<ADBean> datas, int pos) {
        mDatas = datas;
        addIndicator(mDatas, pos);
        mADPagerAdapter.setData(mDatas);
        mViewPager.setCurrentItem(pos);
        setTitleText(pos, false);
    }
    public void startPlayer() {
        if (mADVidoVeiw != null) {
            mADVidoVeiw.startPlay();
        }
    }
    public void stopPlayer() {
        if (mADVidoVeiw != null) {
            mADVidoVeiw.stopPlayer();
        }
    }
    private void setADVideoView(View view) {
        if (view instanceof ADVidoVeiw) {
            mADVidoVeiw = (ADVidoVeiw) view;
        } else {
            mADVidoVeiw = null;
        }
    }

    private void setTitleText(int pos, boolean isAnimation) {
        mSubTitle.setText(mDatas.get(pos).getSubtitle());
        mTitle.setText(mDatas.get(pos).getTitle());
        if (mDatas.get(pos).getAnimation() == 0 || isAnimation) {
            mSubTitle.startAnim(800);
            mTitle.startAnim(800);
        }
        if (mDatas.get(pos).getAnimation() == 0) {
            for (ADBean bean : mDatas) {
                bean.setAnimation(1);
            }
        }
    }
    private void addIndicator(List<ADBean> beans, int pos) {
        if (mIndicatorLayout.getChildCount() != 0) {
            mIndicatorLayout.removeAllViews();
        }
        for (int i = 0; i < beans.size(); i++) {
            View v = new View(getContext());
            if (i == pos)
                v.setBackgroundResource(IndicatorSelectedColor);
            else
                v.setBackgroundResource(IndicatorUnSelectedColor);
            if (lp == null) {
                lp = new LinearLayout.LayoutParams(dip2px(7), dip2px(7));
                lp.setMargins(0, 0, dip2px(5), 0);
            }
            mIndicatorLayout.addView(v, lp);
        }
    }

    private void setIndicator(int pos) {
        int count = mIndicatorLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            if (i == pos) {
                mIndicatorLayout.getChildAt(i).setBackgroundResource(IndicatorSelectedColor);
            } else {
                mIndicatorLayout.getChildAt(i).setBackgroundResource(IndicatorUnSelectedColor);
            }
        }
    }

    private int dip2px(float dipValue) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mADVidoVeiw != null) {
            mADVidoVeiw = null;
        }
        super.onDetachedFromWindow();
    }

    public interface OnClickListaner {
        void onItemClick(ADBean bean);

        void onViewClick();
    }

    public void addOnClickListaner(OnClickListaner l) {
        this.l = l;
    }

    /**
     * view点击
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_adwidget_search:
                if (l != null) {
                    l.onViewClick();
                }
                break;
        }
    }

    /**
     * pagerItem 点击监听
     */
    @Override
    public void onInteraction(int type, Object o) {
        if (l != null) {
            l.onItemClick((ADBean) o);
        }
    }

    /**
     * pager切换监听
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        position = position % mDatas.size();
    }

    @Override
    public void onPageSelected(int position) {
        setADVideoView(mViewPager.getFocusedChild());
        position = position % mDatas.size();
        setIndicator(position);
        setTitleText(position, true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
